<?php


/**
 * 
 * 工具类的函数
 * 
 */
function math($m) {
	$p = '/<div[^>]*style=\"font-size:1\.4em;line-height:22px;[^>]*>([^<]*)<\/div>/';
	$url = "http://www.baidu.com/s?tn=monline_5_dg&bs=1%2B1%2B2%3D%3F&f=8&rsv_bp=1&rsv_spt=1&wd=" . rawurlencode($m);
	$header = array (
		"Content-Type: text/xml;charset=gb2312"
	);
	$do = new CurlUtil($url);
	$do->header = $header;
	$do->init();
	$output = mb_convert_encoding($do->getBody(), 'UTF-8', 'gb2312');
	preg_match($p, $output, $n);
	if ($n)
		return str_replace("&nbsp;", "", $n[1]);
	else
		return "";
}

function acVedio($cp) {
	if (preg_match('/[aA][Cc]([0-9]+)/is', trim($_POST[Message]), $n)) {
		if (@ preg_match('/^@.*/is', trim($_POST[Message]), $x)) {

			return "";
		}
		if (@ preg_match('/\[Image\{[^}].*' . $n[1] . '/is', trim($_POST[Message]), $t)) {

			return "";
		}
		
		$url = "http://www.acfun.tv/v/ac{$n[1]}";
		$flag = false;
		if(!strpos("yaya".$_POST[Message],$url)) {
			$flag = true;
		}
		$do = new CurlUtil($url);
		$do->init();
		$output = $do->getBody();
		//echo $output;
		$p = "/<div id=\"area-title\">.*?<h1 id=\"txt-title-view\" class=\"title\" title=\"视频标题\">([^<]*)<.*?<a class=\"name\"[^>]*>([^<]*)<.*?<span class=\"time\"[^>]*>([^<]*)</is";
		$p2 = "/<div id=\"area-title-view\">.*?<h1 id=\"txt-title-view\">([^<]*)<.*?<a[^>]+class=\"name\"[^>]*>([^<]*)<.*?<span class=\"time\"[^>]*>([^<]*)</is";
///is";//
		preg_match($p2, $output, $m);
       // print_r($m);
		if (!$m) {
			return "";
		}
		//return ;
		$do->url = "http://www.acfun.tv/content_view.aspx?contentId={$n[1]}";
		$do->init();
		$output = $do->getBody();
		$cc = @ explode(',', substr($output, 1, strlen($output) - 2));
		if(trim($m[2])=="卿卿酱") { 
			loginAcfun("傲娇索妮娅酱","123456789",$n[1]);
		}
		
		if($cc[1]=="0") {
			$cookes_file = ROOT_PATH . "/Temp/" .md5("傲娇索妮娅酱"."123456789") . ".dat";
			//echo $cookes_file;
			//if (!file_exists($cookes_file)) {
				echo "沙发已经拿下~~\n";
				$post_fields = "username=" . URLEncode("傲娇索妮娅酱") . "&password=" . URLEncode("123456789");
				$do = new CurlUtil("http://www.acfun.tv/login.aspx");
				$do->cookie_file = $cookes_file;
				$do->post_fields = $post_fields;
				$do->header = 1;
				$do->init();
				$out = $do->getBody();
			//}
			$post_fields2 = "text=" . URLEncode("沙发~") . "&contentId=" . $n[1];
		//	echo $post_fields2;
			$do = new CurlUtil("http://www.acfun.tv/comment.aspx");
			$do->cookie_file = $cookes_file;
			$do->post_fields = $post_fields2;
			$do->init();
			$output = $do->getBody();
		}
		return "视频/文章信息\n" .
		"标题\t\t{$m[1]}\t\nUp主\t\t{$m[2]}\n" .
		"投稿时间\t{$m[3]}\t\n围观人次\t{$cc[0]}\n" .
		"吐槽数量\t{$cc[1]}\t\n收藏数量\t{$cc[5]}\n" .
		"我要吐槽\t@回复 ac{$n[1]}  (内容)".($flag?"\n".$url:"");

	}
}

function getAcfunCid($acid, $l) {
	$url = "http://www.acfun.tv/comment_list_json.aspx?contentId={$acid}&time=" . time(); //http://www.acfun.tv/zj/
	$do = new CurlUtil($url);
	$do->init();
	$a = @ json_decode($do->getBody());
	$count = $a->totalCount;
	
	if (!$l) {
		return array (
			0,
			$count
		);
	}
	if(strlen($l)>6) {
		return array (
			$l,
			$count
		);
	}
	if ($l > $count) {
		return false;
	}
	
	$pagepr = ceil(($count - $l +0.001) / 50.0);
	if ($pagepr == 1) {
		return array (
			$a->commentList[$count - $l],
			$count
		);

	}
	$url = "http://www.acfun.tv/comment_list_json.aspx?contentId={$acid}&currentPage={$pagepr}&time=" . time(); //http://www.acfun.tv/zj/
	$do->url = $url;
	$do->init();
	$output = $do->getBody();
	$a = @ json_decode($output);
	return array (
		$a->commentList[$count - $l - ($pagepr -1) * 50],
		$count
	);
}

function getAcface($mess) {
	$p = '/\[Image\{[^}]*\}[^\]]*\]/';
	preg_match_all($p, $mess, $m);
	foreach ($m[0] as $value) {
		$mess = str_replace($value, getface($value), $mess);
	}
	return $mess;
}

function getface($k) {
	$ret = @ DB :: LimitQuery('td_acface', array (
		'condition' => array (
			'key' => $k
		)
	), true);
	if ($ret) {
		return $ret[0][acface];
	} else {
		return getRandonImage();
	}
}

function getRandFace($k) {
	$ret = @ DB :: LimitQuery('td_acface', array (
		'condition' => array (
			'key' => $k
		)
	), true);
	$index = rand(1, count($ret)) ;
	if ($ret) {
		return $ret[$index][acface];
	} 
}

function getRandonImage() {
	$index = rand(1, 50);
	$imager = "[emot=ac,{$index}/]";
	return $imager;
}

/**
 * 自定义输出函数 调试模式才会输出
 */
function myEcho($mess) {
	if (DEBUG && $_POST[Sender] == "261589289") {
		echo $mess . "\n";
	}
}

function messageReplace($mess) {
	global $replace_arr;
	
	$replace_arr['ac'] =  getface(rand(1, 50)) ;
	$replace_arr['meng'] = getRandFace('meng');
	foreach ($replace_arr as $key => $value) {

		$mess = str_replace('{' . $key . '}', $value, $mess);
	}
	if($_POST[Sender]!="261589289") {
		//return $mess ;
	}
	$p = '/@((\s+\S+)\s*|(\S+)\s*)/is';
	if(preg_match($p, $mess, $m)) {
		//print_r($m);
		$r = loaderClusterMenb();
		//print_r($r);
		if($r) {
			
			 for($i=0;$i<count($r);$i++) {
			 	$a =  strpos($r[$i]->nick, trim($m[1]));
			 	if($a || $a===0 ||$a>0) {
			 		
			 		$mess = preg_replace($p,"[@:".$r[$i]->uin."]",$mess);
			 		break;
			 	}
			 }
		}
	}
	return $mess;
}

function loginAcfun($username, $password, $cid) {
	$post_fields = "username=" . URLEncode($username) . "&password=" . URLEncode($password);
	$cookes_file = ROOT_PATH . "/Temp/" . $_POST[Sender] . ".dat";
	if (!file_exists($cookes_file)) {
		$do = new CurlUtil("http://www.acfun.tv/login.aspx");
		$do->cookie_file = $cookes_file;
		$do->post_fields = $post_fields;
		$do->header = 1;
		$do->init();
		$out = $do->getBody();
	}
	$do = new CurlUtil("http://www.acfun.tv/member/collect.aspx?cId={$cid}&operate=1");
	$do->cookie_file = $cookes_file;
	$do->init();
	$do->getBody();
}

function getreClusterNum($a) {
	$b = str_split($a);
	$s = "";
	$count = count($b);
	for ($i = 0; $i < $count; $i++) {
		if ($i < 3 || $i >= ($count -3))
			$s = $s .
			$b[$i];

		if ($i >= 3 && $i < ($count -3))
			$s = $s .
			"*";

	}
	return $s;
}

function getClusterNameBynum($num) {
	$ret = DB :: GetTableRow("td_cluster_favorable", array (
		"clusternum" => $num
	));

	if ($ret) {
		return $ret[clustername] == "" ? "未获取名称" : $ret[clustername];
	} else {
		return "未获取名称";
	}
}

function createCheckCode() {
	for ($i = 0; $i < 6; $i++) {
		$number = rand(0, 2);
		switch ($number) {
			case 0 :
				$rand_number = rand(48, 57);
				break; //数字
			case 1 :
				$rand_number = rand(65, 90);
				break; //大写字母
			case 2 :
				$rand_number = rand(97, 122);
				break; //小写字母
		}
		$asc = sprintf("%c", $rand_number);
		$asc_number = $asc_number . $asc;
	}
	return $asc_number;
}

function getClusterNameById($cluster) {
	
			$ret = DB :: GetQueryResult("select * from td_view_fav where is_in_cluster=1 and clusternum ='{$cluster}' ", true); //
			return $ret['clustername'] == "" ? "未获取群名字" : $ret[clustername];
}

function loaderClusterMenb() {
	$cFile = ROOT_PATH . "/Temp/clus" .md5($_POST[ClusterNum]) . ".dat";
	if(!file_exists($cFile)) {
		$cookes_file = ROOT_PATH . "/Temp/qq" .md5("1004634672") . ".dat";
		$tempFile = './temqq.txt' ;
		$res =  file_get_contents($cookes_file);
		 $p = '/\bskey\b[\s]*(\S+)\b/is';
		 preg_match($p, $res, $n);
		 $f = ROOT_PATH."Node/gtk.js ".$n[1];
		 $gt_k =  exec("node ".$f);
		 $do = new CurlUtil("http://qun.qzone.qq.com/cgi-bin/get_group_member?uin=1004634672&groupid={$_POST[ClusterNum]}&g_tk={$gt_k}&random=0.".time());
		 $do->cookie_file = $cookes_file;
		 $do->init();
		 $out =   $do->getBody();
		 //echo $out;
		 $p = '/_Callback\((.*?)\);/is';
		 preg_match($p, $out, $m);
		 $fp = fopen($cFile,'ab');
		 fwrite($fp, $m[1]); 
		 fclose($fp); 
	}
	
	$out =  file_get_contents($cFile);
	//echo $out;
	$re = @json_decode($out);
	if($re && $re->data && $re->data->item ) {
		return $re->data->item ;
	} else {
		unlink($cFile) ;
		return false;
	}
}



?>
